<?php

namespace addons\TfFutures\api\controllers;

use addons\TfFutures\common\models\ArticleCate;
use addons\TfFutures\common\models\Article;
use common\helpers\ResultDataHelper;
use Yii;
use api\controllers\OnAuthController;

class ArticleController extends OnAuthController
{
    public $modelClass = Article::class;

    //不用登陆验证的方法
    protected $optional = ['list', 'detail','new','notice'];

    /**
     * 文章列表
     */
    public function actionList()
    {

        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, '请求的方法有误');
        }
        $id = Yii::$app->request->get('id');
        //echo $id;die;
        if (empty($id)) {
            return ResultDataHelper::api(422, "id不能为空");
        }
        //查询是否有此分类
        $model = ArticleCate::find()
                            ->select('id')
                            ->where(['id' => $id, 'status' => 1])
                            ->one();
        if (empty($model)) {
            return ResultDataHelper::api(422, "暂无数据");
        }
        //查询此分类下的文章标题
        $article = Article::find()
                          ->where(['status' => 1, 'cate_id' => $model->id])
                          ->orderBy('created_at desc')
                          ->asArray()
                          ->all();
        if(empty($article)){
            return ResultDataHelper::api(422, "暂无数据");
        }
        foreach ($article as $key => $val) {
            $article[$key]['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
            $article[$key]['updated_at'] = date('Y-m-d H:i:s', $val['created_at']);
        }
        return ResultDataHelper::api(200, $model->id, $article);
    }

    /**
     * 公告列表
     */
    public function actionNotice()
    {

        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, '请求的方法有误');
        }
        //$id = Yii::$app->request->get('id');
        //if (empty($id)) {
        //    return ResultDataHelper::api(422, "id不能为空");
        //}
        //查询是否有此分类
        $model = ArticleCate::find()
            ->select('id')
            ->where(['id' => 89, 'status' => 1])
            ->one();
        if (empty($model)) {
            return ResultDataHelper::api(422, "暂无数据");
        }
        //查询此分类下的文章标题
        $article = Article::find()
            ->where(['status' => 1, 'cate_id' => $model->id])
            ->orderBy('created_at desc')
            ->asArray()
            ->all();
        if(empty($article)){
            return ResultDataHelper::api(422, "暂无数据");
        }
        foreach ($article as $key => $val) {
            $content = htmlspecialchars_decode($val['content']);
            //$content = str_replace(" ", "", $content);
            $article[$key]['content'] = strip_tags($content);
            $article[$key]['created_at'] = date('Y-m-d H:i:s', $val['created_at']);
            $article[$key]['updated_at'] = date('Y-m-d H:i:s', $val['created_at']);
        }
        //echo '<pre>';
        //print_r($article);die;
        return ResultDataHelper::api(200, $model->id, $article);
    }

    public function actionNew(){
        $id = Yii::$app->request->get('id');
        $article = ArticleCate::find()
            ->where(['pid' => $id])
            ->asArray()
            ->all();

        foreach ($article as $key => $val) {
            $article[$key]['name'] = $article[$key]['title'] ;
        }
        return ResultDataHelper::api(200, '成功', $article);

    }

    /**
     * @return mixed
     * 查看文章详情
     */
    public function actionDetail()
    {
        if (!Yii::$app->request->isGet) {
            return ResultDataHelper::api(304, '请求的方法有误');
        }
            //文章id
        $id   = Yii::$app->request->get('id');
        if (empty($id)){
            return ResultDataHelper::api(304, 'id不能为空');
        }
        $bvex = Article::find()
                   ->where(['id' => $id, 'status' => 1])
                   ->orderBy('sort desc')
                   ->one();
        if (empty($bvex)){
            return ResultDataHelper::api(422, '数据为空');
        }
        $bvex->created_at = date('Y-m-d H:i:s', $bvex->created_at);
        $bvex->updated_at = date('Y-m-d H:i:s', $bvex->updated_at);

        return ResultDataHelper::api(200, ['success'], $bvex);

    }
}